package org.lhh.server.core.codec.steps;

import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.StepErrorMeta;
import org.pentaho.di.trans.step.StepMeta;

import java.util.HashMap;
import java.util.Map;

public class StepErrorMetaCodec {

	public static Map<String, String> encode(StepErrorMeta stepErrorMeta) {
		Map<String, String> map = new HashMap<>();
		map.put("source_step", stepErrorMeta.getSourceStep() != null ? stepErrorMeta.getSourceStep().getName() : "");
		map.put("target_step", stepErrorMeta.getTargetStep() != null ? stepErrorMeta.getTargetStep().getName() : "" );
		map.put("is_enabled", stepErrorMeta.isEnabled() ? "Y" : "N");
		map.put("nr_valuename", stepErrorMeta.getNrErrorsValuename());
		map.put("descriptions_valuename", stepErrorMeta.getErrorDescriptionsValuename());
		map.put("fields_valuename", stepErrorMeta.getErrorFieldsValuename());
		map.put("codes_valuename", stepErrorMeta.getErrorCodesValuename());
		map.put("max_errors", stepErrorMeta.getMaxErrors());
		map.put("max_pct_errors", stepErrorMeta.getMaxPercentErrors());
		map.put("min_pct_rows", stepErrorMeta.getMinPercentRows());
		return map;
	}

	public static StepErrorMeta decode( TransMeta transMeta, Map<String, String> map){
		String source_step = map.get("source_step");
		String target_step = map.get("target_step");
		StepMeta sourceStep = transMeta.findStep(source_step);
		StepMeta targetStep = transMeta.findStep(target_step);
		StepErrorMeta stepErrorMeta = new StepErrorMeta(transMeta, sourceStep, targetStep);
		stepErrorMeta.setEnabled("Y".equalsIgnoreCase(map.get("is_enabled")));
		stepErrorMeta.setNrErrorsValuename(map.get("nr_valuename"));
		stepErrorMeta.setErrorDescriptionsValuename(map.get("descriptions_valuename"));
		stepErrorMeta.setErrorFieldsValuename(map.get("fields_valuename"));
		stepErrorMeta.setErrorCodesValuename(map.get("codes_valuename"));
		stepErrorMeta.setMaxErrors(map.get("max_errors"));
		stepErrorMeta.setMaxPercentErrors(map.get("max_pct_errors"));
		stepErrorMeta.setMinPercentRows(map.get("min_pct_rows"));
		return stepErrorMeta;
	}

}

